package edu.gatech.cs2340.mule.view;

import java.awt.Color;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import edu.gatech.cs2340.mule.model.Race;
import edu.gatech.cs2340.mule.presenter.MuleGamePresenter;

/**
 * 
 * Allows the players to configure their player for the game (name, color, etc.)
 * 
 * @author JR O'Donnell
 *
 */
public class CharacterSetUpFrame extends JFrame {
	
	private JPanel contentPane;
	private JTextField nameTextField;
	private	JButton okButton;
	private int currentPlayer;
	private JComboBox raceComboBox;
	private JComboBox colorComboBox;
	
	/**
	 * Create the frame.
	 */
	public CharacterSetUpFrame() {
		currentPlayer = 1;
		setVisible(true);
		setTitle("M.U.L.E.");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(6, 6, 149, 266);
		panel.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		panel.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblName = new JLabel("<html><font size=+1>Name");
		lblName.setBounds(0, 38, 86, 16);
		lblName.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		lblName.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel.add(lblName);
		
		nameTextField = new JTextField("Player 1");
		nameTextField.setBounds(0, 56, 93, 28);
		nameTextField.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		nameTextField.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel.add(nameTextField);
		nameTextField.setColumns(10);
		
		JLabel lblPlayer = new JLabel("<html><font size=+1>Player Creation");
		lblPlayer.setBounds(0, 0, 206, 36);
		lblPlayer.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		lblPlayer.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel.add(lblPlayer);
		
		JLabel lblRace = new JLabel("<html><font size=+1>Race");
		lblRace.setBounds(0, 98, 61, 16);
		lblRace.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		lblRace.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel.add(lblRace);
		
		//Combo Box to select Race
		raceComboBox = new JComboBox();
		raceComboBox.setModel(new DefaultComboBoxModel(new String[]
				{"<html><font size=+1>Human",
				"<html><font size=+1>Flapper",
				"<html><font size=+1>Bonzoid",
				"<html><font face=bold>Ugaite",
				"<html><font size=+1>Buzzite"}));
		raceComboBox.setBounds(0, 116, 99, 27);
		raceComboBox.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		raceComboBox.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel.add(raceComboBox);
		
		JLabel lblColor = new JLabel("<html><font size=+1>Color");
		lblColor.setBounds(0, 160, 61, 16);
		lblColor.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		lblColor.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel.add(lblColor);
		
		//Combo Box to select Color
		colorComboBox = new JComboBox();
		colorComboBox.setModel(new DefaultComboBoxModel(new String[] {
				"<html><font color=red size=+1>Red", "<html><font color=blue size=+1>Blue",
				"<html><font color=white size=+1>White", "<html><font color=black size=+1>Black",
				"<html><font color=orange size=+1>Orange", "<html><font color=#9933CC size=+1>Purple",
				"<html><font color=#008000 size=+1>Green", "<html><font color=#FF66CC size=+1>Pink"}));
		colorComboBox.setBounds(0, 178, 93, 27);
		colorComboBox.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		colorComboBox.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel.add(colorComboBox);
		
		JPanel panel2 = new JPanel();
		panel2.setBounds(100, 6, 299, 266);
		panel2.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		panel2.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		contentPane.add(panel2);
		panel2.setLayout(null);
		
		okButton = new JButton("<html><font size=+1>OK");
		okButton.setBounds(66, 155, 117, 29);
		okButton.setForeground(MuleGamePresenter.BACKGROUND_COLOR);
		okButton.setBackground(MuleGamePresenter.FOREGROUND_COLOR);
		panel2.add(okButton);
		
	}
	
	/**
	 * Method to add an ActionListener to the OK button. To be listened from the presenter
	 * 
	 * @param a the new ActionListener to be applied to the button
	 */
	public void registerOK(ActionListener a) {
		okButton.addActionListener(a);
	}
	
	/**
	 * 
	 * @param index The index of the picked color
	 */
	public void removePickedColor(int index) {
		colorComboBox.removeItemAt(index);
	}
	
	/**
	 * 
	 * @return The index of the selected color
	 */
	public int getPickedColorIndex() {
		return colorComboBox.getSelectedIndex();
	}
	
	/**
	 * 
	 * @return The name of the player in the text field
	 */
	public String getName() {
		if(nameTextField.getText().length() == 0) {
			return "Player " + currentPlayer;
		}
		else {
			return nameTextField.getText();
		}
	}
	
	/**
	 * 
	 * @return The race the user selected for the player
	 */
	public Race getRace() {
		String text = (String) (raceComboBox.getSelectedItem());
		return new Race(text.substring(text.lastIndexOf('>') + 1, text.length()));
	}
	
	/**
	 * 
	 * @return The Color the user selected for the player
	 */
	public Color getColor() {
		if(((String)colorComboBox.getSelectedItem()).equals("<html><font color=red size=+1>Red")) {
			return new Color(255,0,0);		//red
		}
		else if(((String)colorComboBox.getSelectedItem()).equals("<html><font color=blue size=+1>Blue")) {
			return new Color(0,0,255);		//blue
		}
		else if(((String)colorComboBox.getSelectedItem()).equals("<html><font color=white size=+1>White")) {
			return new Color(255,255,255);	//white
		}
		else if(((String)colorComboBox.getSelectedItem()).equals("<html><font color=black size=+1>Black")) {
			return new Color(0,0,0);		//black
		}
		else if(((String)colorComboBox.getSelectedItem()).equals("<html><font color=orange size=+1>Orange")) {
			return new Color(255,128,0);	//orange
		}
		else if(((String)colorComboBox.getSelectedItem()).equals("<html><font color=#9933CC size=+1>Purple")) {
			return new Color(153,51,204);	//purple
		}
		else if(((String)colorComboBox.getSelectedItem()).equals("<html><font color=#008000 size=+1>Green")) {
			return new Color(0,128,0);		//green
		}
		else if(((String)colorComboBox.getSelectedItem()).equals("<html><font color=#FF66CC size=+1>Pink")) {
			return new Color(255,102,204);	//pink
		}
		
		return new Color(255,0,0); //return red if no color is selected
	}
	
	/**
	 * Sets all the components to their defaults
	 */
	public void clearEntries() {
		nameTextField.setText("Player " + (currentPlayer + 1));
		raceComboBox.setSelectedIndex(0);
		colorComboBox.setSelectedIndex(0);
		currentPlayer++;
	}
}
